﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;

namespace TS_XY_Train_Repaire.Stuff
{
    public partial class Stuff_Position_Manage_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public Stuff_Position_Manage_ManaForm()
        {
            InitializeComponent();
        }

        private void Stuff_Position_Manage_ManaForm_Load(object sender, EventArgs e)
        {
            _LoadPosition();
        }

        private void Stuff_Position_Manage_ManaForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }
        private void _LoadPosition()
        {
            LeverString_SysOrganization _ls = new LeverString_SysOrganization();
            tl_Main.Nodes.Clear();
            tl_Main.Refresh();
            ArrayList pList = new ArrayList();
            _ls = new LeverString_SysOrganization();
            _ls.f_id = "H";
            _ls.f_name = "职务字典";
            _ls.f_rbid = TS_Win_Locomotive_MainForm._SysSettings._GetRailwayBureau;
            _ls.f_rsid = TS_Win_Locomotive_MainForm._SysSettings._GetRailwayStations;
            _ls.f_imgindex = 0;
            pList.Add(_ls);
            string strSQL = "select * from TS_S_T_P_Position where f_deep=0 and f_rsid="+TS_Win_Locomotive_MainForm._SysSettings._GetRailwayStations+" and f_rbid="+TS_Win_Locomotive_MainForm._SysSettings._GetRailwayBureau+" and f_del=0 order by f_id asc";
            string _strDepQuery = " and (";
            int i = 0;
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            SqlDataReader read = cmd.ExecuteReader();
            while (read.Read())
            {
                if (i == 0)
                {
                    _strDepQuery += "(f_level like ',%" + read["f_id"].ToString() + "%,')";
                }
                else
                {
                    _strDepQuery += " or (f_level like ',%" + read["f_id"].ToString() + "%,')";
                }
                _ls = new LeverString_SysOrganization();
                _ls.f_id = "P" + read["f_id"].ToString();
                _ls.f_name = read["f_name"].ToString();
                _ls.f_code = read["f_code"].ToString();
                _ls.f_deep = read["f_deep"].ToString();
                _ls.f_level = read["f_level"].ToString();
                _ls.f_rbid = read["f_rbid"].ToString();
                _ls.f_rsid = read["f_rsid"].ToString();
                _ls.f_parent = "H";
                _ls.f_imgindex = 4;
                pList.Add(_ls);
                i++;
            }
            _strDepQuery += ")";
            if (_strDepQuery.Length > 10)
            {
                read.Close();
                strSQL = "select * from TS_S_T_P_Position where f_rsid=" + TS_Win_Locomotive_MainForm._SysSettings._GetRailwayStations + " and f_rbid=" + TS_Win_Locomotive_MainForm._SysSettings._GetRailwayBureau + " " + _strDepQuery + " and f_deep>0 and f_del=0 order by f_id asc";
                cmd = new SqlCommand(strSQL, conn);
                read = cmd.ExecuteReader();
                while (read.Read())
                {
                    _ls = new LeverString_SysOrganization();
                    _ls.f_id = "P" + read["f_id"].ToString();
                    _ls.f_name = read["f_name"].ToString();
                    _ls.f_code = read["f_code"].ToString();
                    _ls.f_deep = read["f_deep"].ToString();
                    _ls.f_level = read["f_level"].ToString();
                    _ls.f_rbid = read["f_rbid"].ToString();
                    _ls.f_rsid = read["f_rsid"].ToString();
                    string _UPID = "";
                    _UPID = read["f_level"].ToString();
                    _UPID = _UPID.Substring(0, _UPID.Length - 1);
                    _UPID = _UPID.Substring(_UPID.LastIndexOf(',') + 1);
                    _ls.f_parent = "P" + _UPID;
                    _ls.f_imgindex = 4;
                    pList.Add(_ls);
                }
            }
            read.Close();
            conn.Close();
            conn.Dispose();

            tl_Main.DataSource = pList;
        }

        private void tb_Name_TextChanged(object sender, EventArgs e)
        {
            bt_Add.Enabled = false;
            bt_Edit.Enabled = false;
            bt_Del.Enabled = false;
            if (tb_Name.Text.Trim().Length > 0 && tb_Code.Text.Trim().Length > 0)
            {
                bt_Add.Enabled = true;
                if (tl_Main.FocusedNode["f_id"].ToString().Substring(0, 1) == "P")
                {
                    bt_Edit.Enabled = true;
                    bt_Del.Enabled = true;
                }
            }
        }

        private void tl_Main_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            tb_Name.Text = "";
            tb_Code.Text = "";
            try
            {
                if (tl_Main.FocusedNode["f_id"].ToString().Substring(0, 1) == "P")
                {
                    tb_Name.Text = tl_Main.FocusedNode["f_name"].ToString();
                    tb_Code.Text = tl_Main.FocusedNode["f_code"].ToString();
                }
                else
                {
                    tb_Name.Text = "";
                    tb_Code.Text = "";
                }
            }
            catch
            {
            }
        }

        private void bt_Add_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            string _UPID = "-1";
            try
            {
                _UPID = tl_Main.FocusedNode["f_id"].ToString();
            }
            catch { }
            if (tl_Main.FocusedNode["f_id"].ToString().Substring(0, 1) == "P")
            {
                strSQL = "insert into TS_S_T_P_Position (f_name,f_code,f_rbid,f_rsid,f_deep,f_level,f_workerid) values('" + tb_Name.Text.Trim() + "','" + tb_Code.Text.Trim() + "'," + TS_Win_Locomotive_MainForm._SysSettings._GetRailwayBureau + "," + TS_Win_Locomotive_MainForm._SysSettings._GetRailwayStations + "," + (Convert.ToInt32(tl_Main.FocusedNode["f_deep"].ToString()) + 1).ToString() + ",'" + tl_Main.FocusedNode["f_level"].ToString() + tl_Main.FocusedNode["f_id"].ToString().Substring(1) + ",'," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
            }
            else
            {
                strSQL = "insert into TS_S_T_P_Position (f_name,f_code,f_rbid,f_rsid,f_deep,f_level,f_workerid) values('" + tb_Name.Text.Trim() + "','" + tb_Code.Text.Trim() + "'," + TS_Win_Locomotive_MainForm._SysSettings._GetRailwayBureau + "," + TS_Win_Locomotive_MainForm._SysSettings._GetRailwayStations + ",0,','," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
            }
            TS_Win_Class.MySqlCom(strSQL);
            //MessageBox.Show(strSQL);
            _LoadPosition();
            try
            {
                tl_Main.SetFocusedNode(tl_Main.FindNodeByFieldValue("f_id", _UPID));
            }
            catch { }
        }

        private void bt_Edit_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            string _UPID = "-1";
            try
            {
                _UPID = tl_Main.FocusedNode["f_id"].ToString();
            }
            catch { }
            strSQL = "update TS_S_T_P_Position set f_name='" + tb_Name.Text.Trim() + "',f_code='" + tb_Code.Text.Trim() + "',f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + tl_Main.FocusedNode["f_id"].ToString().Substring(0, 1);
            TS_Win_Class.MySqlCom(strSQL);
            _LoadPosition();
            try
            {
                tl_Main.SetFocusedNode(tl_Main.FindNodeByFieldValue("f_id", _UPID));
            }
            catch { }
        }

        private void bt_Del_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            string _UPID = "-1";
            try
            {
                _UPID = tl_Main.FocusedNode.PrevNode["f_id"].ToString();
            }
            catch { }
            strSQL = "update TS_S_T_P_Position set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + tl_Main.FocusedNode["f_id"].ToString().Substring(1);
            TS_Win_Class.MySqlCom(strSQL);
            _LoadPosition();
            try
            {
                tl_Main.SetFocusedNode(tl_Main.FindNodeByFieldValue("f_id", _UPID));
            }
            catch { }
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }
}